<?php

class Custom_Controller_Plugin_Gather extends Zend_Controller_Plugin_Abstract
{
	
	
	
	
	public function getFirstImage($content){
		$pattern='/<img([\s\S]+?)src=(?:\"|\')([\s\S]+?)(?:\"|\')([\s\S]*?)([\/])?>/i';
		preg_match($pattern,$content,$matches);
		return $matches[2];
	}
	
	
	
	
	/**
	 * 把内容中运程图片进行本地化
	 * Enter description here ...
	 * @param unknown_type $preg  eg:
	 * @param unknown_type $content
	 */
	public function doImageContent($content){
		
		$pattern='/<img([\s\S]+?)src=(?:\"|\')([\s\S]+?)(?:\"|\')([\s\S]*?)([\/])?>/i';
		return $content=preg_replace_callback($pattern,array(&$this, 'fetchImage'),$content);
	}
	
	
	/**
	 * 去掉非站内链接
	 * Enter description here ...
	 * @param unknown_type $content
	 */
	public function stripOtherUrl($content){
		$pattern='/<a([\s\S]+?)href=(?:\"|\')([\s\S]+?)(?:\"|\')([\s\S]*?)>([\s\S]+?)<\/a>/i';
		return $content=preg_replace_callback($pattern,array(&$this, 'stripUrl'),$content);
	}
	
	
	/**
	 * 取到匹配链接的图片元素
	 * Enter description here ...
	 * @param unknown_type $matches
	 */
	public function stripUrl($matches){
		
		$findstring=preg_replace('/http:\/\//','', WEB_NAME);
		$string=$matches[2];
		$pos = strpos($string,$findstring);
		if($pos=== false){
			return $matches[4];
		}else{
			return $matches[0];
		}
	}
	
	/**
	 * 
	 * 返回处理后的html图片
	 *
	 * Enter description here ...
	 * @param unknown_type $matches //匹配的图片元素
	 * @param unknown_type $thumb 是否加水印
	 */
	public  function fetchImage($matches){	
	
		$newpirctrue=array();
		$Folder	= new Custom_Controller_Plugin_Folder();
		$pictures=$filelist=array();
		$filesuffix=array('jpeg','bmp','jpg','gif','png');
		$url=$matches[2];
		$suffix=$Folder->getSuffix($url);
		//检查是否为图片
		if(in_array($suffix, $filesuffix)){	
			$randdir=$Folder->randDir();//取到目录类似：/3d/d6
			$newdir=THUMB_ARTICLE_DIR.$randdir;//本地目录D:/XX
            $newname=$Folder->getNewName($url);//重新命名图片
            $target=$newdir.$newname;
			$result=$this->_fetchImg($url,$target);
			if($result){
				//if($thumb){
					$Watermark=new Custom_Controller_Plugin_Image($target);
					$Watermark->setWaterImage(WATERMARK_NAME_DIR);
					$Watermark->getWatermark();
				//}
				return '<img '.$matches[1].' src="'.ARTICLE_DIR.$randdir.$newname.'"  '.$matches[3].'/>';
			}else{
				return false;
			}
		}
		
	}
	
	
	
 	/**
     * 下载远程图片
     * 把远程图片能过curl存到本地
     * @param unknown_type $url
     * @param unknown_type $target
     */
    public	function	_fetchImg($url,$target){
    	$ch = curl_init();  
		curl_setopt($ch, CURLOPT_URL, $url);  
		curl_setopt($ch, CURLOPT_GET, 1);  
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);  
		curl_setopt($ch, CURLOPT_TIMEOUT, 30);  
		$images = curl_exec($ch);  
		curl_close($ch);  
		//保存图片  
		$tp = @fopen($target, 'wb');  
		$result=fwrite($tp, $images);  
		fclose($tp);
		return $result;
    }
    
   /**
    * 把文件移动到新的目录
    * Enter description here ...
    * @param unknown_type $array //文件数组
    * @param unknown_type $dir //新目录
    */
    public function moveArrayFile($files,$dir){
    
    	$Folder=new Custom_Controller_Plugin_Folder();
    
    	if(is_array($files)){
    		foreach ($files as $val){
    			$tmpdir=PROJECT_ROOT.$val;
    			$randdir=$Folder->randDir();
    			$newdir=$dir.$randdir;
    			$result = $Folder->createDir($newdir);
				if($result){
					$newname=$Folder->getNewName($val);
					@$flag=rename($tmpdir,$newdir.$newname);
					if($flag)$newfile[]=$randdir.$newname;
				}else{
					echo 'Created Dir Error ';
					exit;
				}
    
    
    			
    		}
    	}else{
    		$tmpdir=PROJECT_ROOT.$files;
    		$randdir=$Folder->randDir();
    		$newdir=$dir.$randdir;
			
    		$result = $Folder->createDir($newdir);
			if($result){
				$newname=$Folder->getNewName($files);
				@$flag=rename($tmpdir,$newdir.$newname);
				if($flag)$newfile=$randdir.$newname;
			}else{
				echo 'Created Dir Error ';
				exit;
			}
    	}
    
    	return $newfile;
    }
    
    /**
     * 把文件移动到新的目录 //大图
     * Enter description here ...
     * @param unknown_type $array //文件数组
     * @param unknown_type $dir //新目录
     */
    public function moveArrayFileLarge($files,$dir){
    
    	$Folder=new Custom_Controller_Plugin_Folder();
    
    	if(is_array($files)){
    		foreach ($files as $val){
    			$tmpdir=PROJECT_ROOT.$val;
    			$randdir=$Folder->randDir();
    			$newdir=$dir.$randdir;
    			@$Folder->createDir($newdir);
    			$newname=$Folder->getNewName($val);
    
    			$flag=rename($tmpdir,$newdir."large".$newname);
    
    
    			if($flag)$newfile[]=$randdir."large".$newname;
    		}
    	}else{
    		$tmpdir=PROJECT_ROOT.$files;
    		$randdir=$Folder->randDir();
    		$newdir=$dir.$randdir;
    		@$Folder->createDir($newdir);
    		$newname=$Folder->getNewName($files);
    		@$flag=rename($tmpdir,$newdir."large".$newname);
    		if($flag)$newfile=$randdir."large".$newname;
    	}
    
    	return $newfile;
    }
    
    
   
   
    
    
    private function pagefetch($url){
	
		set_time_limit(0);
		$names=explode('.',basename($url));
		
		$baseid=$names[0];
	    $snoopy = new Custom_Controller_Plugin_Snoopy(); 
	   
		$snoopy->expandlinks=true;
		$android=array();
		$resutls=$snoopy->fetchlinks($url);
		
		$url=array(); 
		$url=$snoopy->results; 
	
		foreach ($url as $key=>$value)	{ 
		//匹配地址  http://www.jiancai365.cn/news/46135_3.htm
		if (!preg_match("/^http:\/\/www.jiancai365\.cn\/news\/".$baseid."_(\d).htm$/i",$value)){ 
			unset($url[$key]); 
		  	} 
		}
		
		$contenthtml='';
		foreach ($url as $key=>$value) {
		 	$snoopy->fetch($value); 
		  	$temp=$snoopy->results;  
		    $android['oldurl']=trim($oldurl);
		    $num=count($temp)-1;
		    $pagecontent=$temp[$num];
			
			//取到内容和标题
			if (preg_match_all("/<td style=\"line-height:180%;font-size:14px\">([\s\S]+?)<div align=center>/i",$pagecontent,$content)){ 

				$contents=strip_tags(trim($content[1][0]),'<p><img><table><span><h1><h2><h3><h4><h5><h6><tr><td><br><br/>');
				
			 }

			 $contenthtml.=iconv("gb2312","UTF-8",$contents);
			 $contents='';
		}
		
		//图片本地化
		$contenthtml=preg_replace_callback("/<[IMG|img]([\s\S]+?)src=\"([\s\S]+?)\"([\s\S]*?)([\/])?>/i",array(&$this, 'jiacaidoimages'),$contenthtml);
		return $contenthtml;
		
	}
    
	
	

}